import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class ClosestCodeMatches {

    public static void getRes() throws FileNotFoundException {

        //用tomcat作为模板  与自己服务器生成的对比

        //输入要查找对比的url
        String[] strings={"MyWebTester\\TomcatTest\\_all_html.txt",
                "MyWebTester\\TomcatTest\\_Login.txt",
                "MyWebTester\\TomcatTest\\_PetServlet.txt",
                "MyWebTester\\TomcatTest\\_PetServlet2.txt",
                "MyWebTester\\TomcatTest\\_show5_jsp.txt",
                "MyWebTester\\TomcatTest\\_test1_html.txt",
                "MyWebTester\\TomcatTest\\_test2_jsp.txt",
                "MyWebTester\\TomcatTest\\_test6_jsp.txt",
        };//输入要查找的url

        //查找的目录
        File file=new File("MyWebTester\\MyServerTest");
        List<String> list=new ArrayList<>();
        findTxtFile(file,list);


        List<String> res=new ArrayList<>();
        List<Double> d=new ArrayList<>();
        for(int i=0;i<strings.length;i++)
        {
            double max=0;
            String b=null;
            for(int j=0;j<list.size();j++)
            {
                if(strings[i].equals(list.get(j))){   //排除原文件
                    continue;
                }
                double cos = CodeFile.getCos(strings[i], list.get(j));
                if(cos>max)
                {
                    b=list.get(j);
                    max=cos;
                }
            }
             res.add(b);
            d.add(max);
        }

        //输出结果
        for(int i=0;i<strings.length;i++)
        {
//            System.out.println("相似度为"+d.get(i));
            String[] split = strings[i].split("\\\\");
            System.out.println(split[split.length - 1]+"    "+d.get(i) );
//            System.out.println("给定的需要找相似文件："+strings[i]);
//            System.out.println("找到的相似度最高："+res.get(i));
        }


    }

    public static void findTxtFile(File file, List<String> list){
        File[] files = file.listFiles();
        for (File file1 : files) {
            if(file1.isDirectory())
            {
                findTxtFile(file1,list);
            }
            else
            {
                if(file1.getName().endsWith(".txt"))
                    list.add(file1.getPath());
            }
        }
    }

}
